﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sirian_ScriptEditor.Dialog;
using System.IO;

namespace Sirian_ScriptEditor
{
    public partial class Form1 : Form
    {
        List<int> times = new List<int>();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            switch (comboType.SelectedIndex) 
            {
                case 0:
                    SetMusic dialogMusic = new SetMusic();
                    dialogMusic.Owner = this;
                    if (dialogMusic.ShowDialog() == DialogResult.OK)
                    {
                        int i = findInsertIndex(dialogMusic.time);
                        times.Insert(i, dialogMusic.time);
                        lstScript.Items.Insert(i, "(" + dialogMusic.time + ")SETMUSIC:MUSICNAME=" + dialogMusic.key);
                    }
                    break;
                case 1:
                    CreateBoss dialogBoss = new CreateBoss();
                    dialogBoss.Owner = this;
                    if (dialogBoss.ShowDialog() == DialogResult.OK)
                    {
                        int i = findInsertIndex(dialogBoss.time);
                        times.Insert(i, dialogBoss.time);
                        lstScript.Items.Insert(i, "(" + dialogBoss.time + ")SETBOSS:AICRAFT=" + dialogBoss.key);
                    }
                    break;
                case 2:
                    CreateWave dialogWave = new CreateWave();
                    dialogWave.Owner = this;
                    if (dialogWave.ShowDialog() == DialogResult.OK)
                    {
                        int i = findInsertIndex(dialogWave.time);
                        times.Insert(i, dialogWave.time);
                        lstScript.Items.Insert(i, "(" + dialogWave.time + ")SETWAVE:FORMATION=" + dialogWave.formation + "; PATH=[" + dialogWave.path + "]; AIRCRAFTS=[" +dialogWave.aircrafts + "]");
                    }
                    break;
            }

        }

        private void comboType_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (lstScript.SelectedIndex > -1)
            {
                times.RemoveAt(lstScript.SelectedIndex);
                lstScript.Items.RemoveAt(lstScript.SelectedIndex);
            }
        }

        private int findInsertIndex(int time)
        {
            int i = 0;
            if (times.Count > 0)
            {
                bool found = false;
                while (i < times.Count && !found)
                {
                    found = times[i] > time;
                    i++;
                }
                if (found) { i--; }
            }
            return i;
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog exportDialog = new SaveFileDialog();
            exportDialog.DefaultExt = "*.scp";
            exportDialog.Filter = "ScriptFiles (*.scp)|*.scp|All files (*.*)|*.*";
            if (exportDialog.ShowDialog() == DialogResult.OK)
            {
                string Path = exportDialog.FileName;
                TextWriter writer = new StreamWriter(Path);
                foreach (string item in lstScript.Items)
                {
                    writer.WriteLine("--" + item.ToString());
                }
                writer.Close();
            }
        }
    }
}
